﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using NUnit.Framework;
using XmlSockets.Internal;
namespace LibXmlSocket_UnitTest
{
    [TestFixture]
    public class XmlMessageWriterUnitTest
    {
        private IXmlSocketSendContent CreateContent(byte[] buffer, long postion)
        {
            return new TestSendContent(buffer, postion);
        }

        [TestCase(300)]
        public void Constructor(int size)
        {
            XmlMessageWriter writer = new XmlMessageWriter(size);
            
        }

        [TestCase(-1)]
        [ExpectedException(typeof(ArgumentException))]
        public void BadConstructor(int size)
        {
            XmlMessageWriter writer = new XmlMessageWriter(size);
        }

        [TestCase(300)]
        [TestCase(0)]
        [TestCase(65536)]
        [ExpectedException(typeof(InvalidOperationException))]
        public void BadCreateContentTest(int length)
        {
            XmlMessageWriter writer = new XmlMessageWriter(length);
            var content = writer.CreateContent(CreateContent);
            Assert.AreEqual(length, content.Buffer.Length);
        }

        [TestCase(300,"testvalue")]
        [TestCase(65536, "testvalue")]
        [TestCase(0,TESTVALUE)]
        public void CraeteContentTest(int length,string value)
        {
            XmlMessageWriter writer = new XmlMessageWriter(length);
            var message = new XElement("testmessage");
            message.SetAttributeValue("testname", value);
            message.WriteTo(writer.InternalWriter);
            writer.InternalWriter.Flush();
            var buffer = writer.CreateContent(CreateContent);
            Assert.AreEqual(message.ToString() + "\0", XmlSocketConstant.XmlSocketEncoding.GetString(buffer.Buffer, 0, buffer.Buffer.Length));
            writer.Clear();
        }


        const string TESTVALUE = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }
}
